<template lang="">
    <div>
        <el-dialog title="修改密码" :visible.sync="dialogVisible" width="45%" :before-close="handleClose">
            <div class="password box-center">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                    class="demo-ruleForm" hide-required-asterisk>
                    <el-form-item label="新密码" prop="pass">
                        <el-input type="password" v-model="ruleForm.newPassword" autocomplete="off" show-password>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" show-password>
                        </el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="emitServer('ruleForm')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        name: 'EditPassword',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.newPassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                dialogVisible: false,
                ruleForm: {
                    newPassword: '',
                    checkPass: '',
                },
                rules: {
                    newPassword: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => { });
            },
            closeDialog() {
                this.dialogVisible = false
            },
            emitServer(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios({
                            method:'PUT',
                            url:'/qssAdmin/save',
                            withCredentials: true,
                            data:JSON.stringify({newPassword:this.ruleForm.newPassword}),
                            header:{
                                'Content-Type': 'application/json'
                            }
                        }).then((result) => {
                            console.log('修改密码',result);
                            if(result.data.msg == '操作失败'){
                                this.$message({
                                    message:result.data.data,
                                    type:'error'
                                })
                            }else if(result.data.msg == '操作成功'){
                                this.$message({
                                    message:'密码修改成功',
                                    type:'success'
                                })
                            }
                        }).catch((err) => {
                            this.$message({
                                message:err.message,
                                type:'info'
                            })
                        });
                    }else{
                        return false
                    }
                })
                this.closeDialog()
            }
        },
        mounted() {
            this.$bus.$on('editPassword', (val) => {
                this.dialogVisible = true
            })
        },
        beforeDestroy() {
            this.$bus.$off('editPassword')
        },
    }
</script>
<style scoped>
    .password {
        width: 500px;
    }

    .el-form {
        transform: translate(-40px);
    }
</style>